﻿using Microsoft.AspNetCore.Mvc;
using STF.Blog.Dtos;
using STF.Blog.IService;
using STF.Blog.WebApp.Areas.ForumManage.Filters;
using STF.Blog.WebApp.Areas.ForumManage.Models.AdminPermissions;
using STF.Blog.WebApp.Areas.ForumManage.Models.Roles;

namespace STF.Blog.WebApp.Areas.ForumManage.Controllers
{
    [Area("ForumManage")]
    [Login]
    public class AdminPermissionsController : Controller
    {
        private IAdminPermissionsService _permissionSvc;
        private IRolesService _rolesSvc;
        private ISystemMenusService _systemMenusSvc;
        public AdminPermissionsController(IAdminPermissionsService permissionSvc, 
                                            IRolesService rolesSvc,
                                            ISystemMenusService systemMenusSvc)
        {
            _permissionSvc = permissionSvc;
            _rolesSvc = rolesSvc;
            _systemMenusSvc = systemMenusSvc;
        }

        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var data = await _rolesSvc.GetAllUsersAsync();  // 查询所有权限信息
            ViewBag.RolesList = data.Select(rl => new RolesListViewModel 
            {
                Id = rl.Id,
                Title = rl.Title
            }).ToList();

            return View();
        }

        [HttpGet]
        public async Task<IActionResult> GetOwnMenu(int rolesId) 
        {
            var sysmenuId = await _permissionSvc.GetSystemMenuIdByRolesId(rolesId);
            var allMenus = await _systemMenusSvc.GetAllUsersAsync();

            var own = GetMenus(allMenus,sysmenuId,1);
            var noOwn = GetMenus(allMenus, sysmenuId,0);

            return Json(new GetMenuListViewModel 
            {
                OwnList = own,
                NoOwnList = noOwn
            });
        }
        
        private List<AdminPermissionsListViewModel> GetMenus(List<SystemMenuDto> all,List<int> menusId, int isExist = 0) 
        {
            if (menusId.Count > 0)
            {
                if (isExist == 1)
                {
                    var menusInfo = all.Where(m => menusId.Contains(m.Id)).ToList();
                    return menusInfo.Select(sm => new AdminPermissionsListViewModel
                    {
                        MenuId = sm.Id,
                        MenuTitle = sm.Title
                    }).ToList();
                }
                else
                {
                    var menusInfo = all.Where(m => !menusId.Contains(m.Id)).ToList();
                    return menusInfo.Select(sm => new AdminPermissionsListViewModel
                    {
                        MenuId = sm.Id,
                        MenuTitle = sm.Title
                    }).ToList();
                }
            }
            else
            {
                if (isExist == 1)
                {
                    return new List<AdminPermissionsListViewModel>();
                }
                else
                {
                    return all.Select(sm => new AdminPermissionsListViewModel
                    {
                        MenuId = sm.Id,
                        MenuTitle = sm.Title
                    }).ToList();
                }
            }
        }

        [HttpPost]
        public async Task<IActionResult> Deletes(int[] menuIdList, int rolesId) 
        {
            var res = 0;
            for (int i = 0; i < menuIdList.Length; i++) 
            {
                var data = await _permissionSvc.GetDataByMenuIdAndRolesIdAsync(menuIdList[i], rolesId);
                if (data != null)
                    res += await _permissionSvc.PutTrashAsync(data.Id);
            }

            return Json(res == menuIdList.Length);
        }

        [HttpPost]
        public async Task<IActionResult> Adds(int[] menuIdList, int rolesId) 
        {
            var res = 0;
            for (int i = 0; i < menuIdList.Length; i++)
            {
                res += await _permissionSvc.AddUsersAsync(rolesId,menuIdList[i]);
            }
            return Json(res == menuIdList.Length);
        }
    }
}
